package PanelTests;

import java.awt.EventQueue;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JEditorPane;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Rectangle;

import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import java.awt.Color;
import java.awt.SystemColor;
import javax.swing.JScrollPane;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.swing.table.*;

import com.contexticon.Listeners.ChangeNotification;
import com.contexticon.Listeners.OurFocusListener;
import com.contexticon.Listeners.OurTableModelListener;
import com.contexticon.Listeners.TableCellListener;
import com.contexticon.TableEditorsRenderers.SimpleMultiRowCellEditor;
import com.contexticon.TableEditorsRenderers.SpinnerEditor;
import com.contexticon.TableEditorsRenderers.TableCellLongTextEditor;
import com.contexticon.TableEditorsRenderers.TableCellLongTextRenderer;
import com.contexticon.logic.TableVerifier;
import com.contexticon.mediaTest.*;
import com.contexticon.model.Speaker;
import com.contexticon.model.Utterance;
import com.contexticon.parser.OurParser;
import com.contexticon.filehandling.TranscriptFileWriter;

import javax.swing.BoxLayout;
import javax.swing.DefaultCellEditor;
import javax.swing.JFileChooser;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;

//import uiTest.PanelTableEditorTest.SimpleMultiRowCellEditor.PanelFocusListener;
//import uiTest.PanelTableEditorTest.SimpleMultiRowCellEditor;

import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.filter.swing.SwingFileFilterFactory;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JLabel;
import javax.swing.border.BevelBorder;
import javax.swing.JSpinner;
import java.awt.BorderLayout;
import javax.swing.JSplitPane;
import javax.swing.JCheckBox;
import com.contexticon.Listeners.*;
import javax.swing.JSeparator;
import javax.swing.JCheckBoxMenuItem;

import java.awt.Toolkit;

public class Transcriber5 extends JFrame {

	
	private JPanel contentPane;
	public static JTable table;
	public static PlayerTest playerTest;
	public static Object[][] metaTable;
	private final Action action = new SwingAction();
	private JFileChooser fileChooser;
	private Toolkit toolKit = Toolkit.getDefaultToolkit();


	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {

		File file = new File("/Applications/VLC.app/Contents/MacOS/lib/");
		if (file.exists()) {
			System.setProperty("jna.library.path",
					"/Applications/VLC.app/Contents/MacOS/lib/");
		} else {
			System.setProperty("jna.library.path",
					"C:\\Users\\Till\\Desktop\\VLC builds\\Win64\\");
		}

		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Transcriber5 frame = new Transcriber5();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Transcriber5() {
		setTitle("Contexticon - Transcriber");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//Position Transcriber on right side of screen
		final Dimension screenSize = toolKit.getDefaultToolkit().getScreenSize();
		setBounds(((screenSize.getSize().width)/2), 0, ((screenSize.getSize().width)/2), screenSize.getSize().height-45);
		//setBounds(100,100,750,500);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);

		JMenuItem mntmOpenMovie = new JMenuItem("Open movie");
		mntmOpenMovie.setAction(action);
		mnFile.add(mntmOpenMovie);

		// saving the transcript
		JMenuItem mntmSaveTranscript = new JMenuItem("Save transcript");
		mntmSaveTranscript.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				String filename = "";
				String dir = "";

				
				// opening a window to let a user choose the file
				JFileChooser c = new JFileChooser();
				// Demonstrate "Save" dialog:
				int rVal = c.showSaveDialog(Transcriber5.this);
				if (rVal == JFileChooser.APPROVE_OPTION) {
					filename = c.getSelectedFile().getName();
					dir = c.getCurrentDirectory().toString();
				}
				if (rVal == JFileChooser.CANCEL_OPTION) {
					System.out.println("You pressed cancel");
				}
								OurParser.writeModelToXML(
						(DefaultTableModel) table.getModel(),
						(dir + "/" + filename+".xml"), null);
				
				// Confirm saving
				JOptionPane.showMessageDialog(c, "Transcript successfully saved.");
				

			}
		});
		
		JMenuItem mntmOpenATranscript = new JMenuItem("Open a transcript");
		mntmOpenATranscript.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				// set file filter for file chooser
				fileChooser.setApproveButtonText("Open");
				final FileNameExtensionFilter xmlFilter = new FileNameExtensionFilter(".xml", "xml");
				fileChooser.addChoosableFileFilter(xmlFilter);
				//fileChooser.addChoosableFileFilter(SwingFileFilterFactory
					//	.newAudioFileFilter());
				//FileFilter defaultFilter = SwingFileFilterFactory.newMediaFileFilter();
				//fileChooser.addChoosableFileFilter(defaultFilter);
				//fileChooser.setFileFilter(defaultFilter);
				fileChooser.setFileFilter(xmlFilter);

				if (JFileChooser.APPROVE_OPTION == fileChooser
						.showOpenDialog(Transcriber5.this)) {

					TableModel model = OurParser.readConfigTable(fileChooser.getSelectedFile()
							.getAbsolutePath());
					
					table.setModel(model);
					table.getColumnModel().getColumn(0).setResizable(false);
					table.getColumnModel().getColumn(0).setPreferredWidth(60);
					table.getColumnModel().getColumn(1).setResizable(false);
					table.getColumnModel().getColumn(1).setPreferredWidth(60);
					table.getColumnModel().getColumn(2).setPreferredWidth(473);
					table.removeColumn(table.getColumn("Fidelity"));
					table.removeColumn(table.getColumn("Speaker"));
					table.removeColumn(table.getColumn("Language tags"));
					table.removeColumn(table.getColumn("Content tags"));
					table.removeColumn(table.getColumn("Transfer tags"));
					table.removeColumn(table.getColumn("Scene"));
					TableVerifier verifier = new TableVerifier(table, playerTest);
					table.getColumnModel().getColumn(2)
					.setCellEditor(new TableCellLongTextEditor());

					
					
				}


			}
		});
		
		mnFile.add(mntmOpenATranscript);
		
		JSeparator separator = new JSeparator();
		mnFile.add(separator);
		mnFile.add(mntmSaveTranscript);
		
		JSeparator separator_1 = new JSeparator();
		mnFile.add(separator_1);
		
		JMenuItem mntmExit = new JMenuItem("Exit");
		mntmExit.setEnabled(false);
		mnFile.add(mntmExit);
		
		JMenu mnEdit = new JMenu("Edit");
		menuBar.add(mnEdit);
		
		JMenuItem mntmMergeUtterances = new JMenuItem("Merge utterances...");
		mntmMergeUtterances.setEnabled(false);
		mnEdit.add(mntmMergeUtterances);
		
		JMenuItem mntmDeleteUtterances = new JMenuItem("Delete utterances...");
		mntmDeleteUtterances.setEnabled(false);
		mnEdit.add(mntmDeleteUtterances);
		
		JMenu mnPlay = new JMenu("Play");
		menuBar.add(mnPlay);
		
		JMenuItem mntmPlayUtterance = new JMenuItem("Play utterance");
		mntmPlayUtterance.setEnabled(false);
		mnPlay.add(mntmPlayUtterance);
		
		JMenuItem mntmLoopUtterance = new JMenuItem("Loop utterance");
		mntmLoopUtterance.setEnabled(false);
		mnPlay.add(mntmLoopUtterance);
		
		JMenu mnView = new JMenu("View");
		menuBar.add(mnView);
		
		JMenuItem mntmIncreaseRowHeight = new JMenuItem("Increase row height");
		mntmIncreaseRowHeight.setEnabled(false);
		mnView.add(mntmIncreaseRowHeight);
		
		JMenuItem mntmDecreaseRowHeight = new JMenuItem("Decrease row height");
		mntmDecreaseRowHeight.setEnabled(false);
		mnView.add(mntmDecreaseRowHeight);
		
		JSeparator separator_3 = new JSeparator();
		mnView.add(separator_3);
		
		JCheckBoxMenuItem chckbxmntmShowPanel = new JCheckBoxMenuItem("Show Panel");
		chckbxmntmShowPanel.setEnabled(false);
		chckbxmntmShowPanel.setSelected(true);
		mnView.add(chckbxmntmShowPanel);
		
		JMenu mnOptions = new JMenu("Options");
		menuBar.add(mnOptions);
		
		JMenuItem mntmCustomize = new JMenuItem("Customize...");
		mntmCustomize.setEnabled(false);
		mnOptions.add(mntmCustomize);
		
		JMenu mnHelp = new JMenu("Help");
		menuBar.add(mnHelp);
		
		JMenuItem mntmKeyboardShortcuts = new JMenuItem("Keyboard Shortcuts");
		mntmKeyboardShortcuts.setEnabled(false);
		mnHelp.add(mntmKeyboardShortcuts);
		
		JMenuItem mntmDocumentation = new JMenuItem("Documentation...");
		mntmDocumentation.setEnabled(false);
		mnHelp.add(mntmDocumentation);
		
		JSeparator separator_2 = new JSeparator();
		mnHelp.add(separator_2);
		
		JMenuItem mntmAbout = new JMenuItem("About");
		mntmAbout.setEnabled(false);
		mnHelp.add(mntmAbout);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		fileChooser = new JFileChooser();
		//Options moved to actions
		//'Open Media File' and 'Open Transcript'
		//to apply different file type filters to each
		
		playerTest = new PlayerTest();

		/**
		 * table.setModel(new DefaultTableModel(new Object[][] {}, new String[]
		 * { "Beginning", "End", "Text", "Fidelity", "Speaker", "LanguageTags",
		 * "ContentTags", "TransferTags", "Uncertainty"}) { Class[] columnTypes
		 * = new Class[] { Float.class, Float.class, String.class, Float.class,
		 * String.class, String.class,String.class,String.class,Boolean.class };
		 * 
		 * public Class getColumnClass(int columnIndex) { return
		 * columnTypes[columnIndex]; } });
		 */

		JScrollPane scrollPane = new JScrollPane();

		table = new JTable();
		table.setToolTipText("Use this table to type or paste the spoken text.\r\nTime codes are set automatically upon hitting\r\nCtrl + ENTER according to the current\r\nplayback time, but can be adjusted by hitting\r\nCtrl + UP/DOWN.");

		table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
		

		
		
		// setting tables model

		table.setModel(new DefaultTableModel(new Object[][] {}, new String[] {

		"Beginning", "End", "Text", "Fidelity", "Speaker", "Language tags" , "Content tags", "Transfer tags", "Scene" }) {
			Class[] columnTypes = new Class[] { Float.class, Float.class,
					String.class, Float.class, Speaker.class, String.class, String.class, String.class, String.class };

			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
		});

		//Fixing width of time code columns
		table.getColumnModel().getColumn(0).setResizable(false);
		table.getColumnModel().getColumn(0).setMinWidth(70); // fixing
		table.getColumnModel().getColumn(0).setMaxWidth(70); // column width
		table.getColumnModel().getColumn(0).setPreferredWidth(60);
		table.getColumnModel().getColumn(1).setResizable(false);
		table.getColumnModel().getColumn(1).setMinWidth(70); // fixing
		table.getColumnModel().getColumn(1).setMaxWidth(70); // column width
		table.getColumnModel().getColumn(1).setPreferredWidth(60);
		table.getColumnModel().getColumn(2).setMinWidth(120);
		table.getColumnModel().getColumn(2).setPreferredWidth(473);
		table.removeColumn(table.getColumn("Fidelity"));
		table.removeColumn(table.getColumn("Speaker"));
		table.removeColumn(table.getColumn("Language tags"));
		table.removeColumn(table.getColumn("Content tags"));
		table.removeColumn(table.getColumn("Transfer tags"));
		table.removeColumn(table.getColumn("Scene"));

		
		playerTest.setModel((DefaultTableModel) table.getModel());

		DefaultTableModel ourModel = (DefaultTableModel) table.getModel();

		ListSelectionModel listSelectionModel = table.getSelectionModel();
		table.setSelectionModel(listSelectionModel);

		// set the margins between the rows
		table.setRowMargin(6);
		table.setRowHeight(16 + 22);
		table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

		TableVerifier verifier = new TableVerifier(table, playerTest);

		// here we set the custom cell renderer

				
		// here we set the custom cell editor
		TableCellLongTextEditor tce = new TableCellLongTextEditor();
		table.getColumnModel().getColumn(2)
				.setCellEditor(tce);
		// adding the right click menu


		/**
		 * this will have to be relocated to inside o
		 * @author dglodzik
		 *
		 */
	

		MouseListener popupListener = new PopupListener();
		table.addMouseListener(popupListener);
		table.getTableHeader().addMouseListener(popupListener);
		
		
		
		// set jspinner as editor for the first 2 columns
		// table.getColumnModel().getColumn(0).setCellEditor(new SpinnerEditor(
		// ));

		// table.addFocusListener(new OurFocusListener());
		scrollPane.setViewportView(table);

		((DefaultTableModel) table.getModel()).addRow(new Object[] {
				new Float(0),null, null, null, null, null, null, null, null });
		
		JPanel panel = new JPanel();
		

		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 440, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 278, GroupLayout.PREFERRED_SIZE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addComponent(panel, GroupLayout.DEFAULT_SIZE, 644, Short.MAX_VALUE)
				.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 644, Short.MAX_VALUE)
		);
		
				JPanel panel_1 = new JPanel();
				panel_1.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null,
						null, null));
				
						JComboBox comboBox = new JComboBox();
						comboBox.setAlignmentX(1.0f);
						
								JLabel label = new JLabel("Fidelity");
								
										JComboBox comboBox_1 = new JComboBox();
										comboBox_1.setAlignmentX(1.0f);
										
												JLabel label_1 = new JLabel("Speaker");
												
														JComboBox comboBox_2 = new JComboBox();
														comboBox_2.setAlignmentX(1.0f);
														
																JLabel label_2 = new JLabel("Language Tags");
																
																		JLabel label_3 = new JLabel("Content Tags");
																		
																				JLabel label_4 = new JLabel("Transfer Tags");
																				
																						JCheckBox checkBox = new JCheckBox("unsure about transcription");
																						
																								JComboBox comboBox_3 = new JComboBox();
																								comboBox_3.setAlignmentX(1.0f);
																								
																										JComboBox comboBox_4 = new JComboBox();
																										comboBox_4.setAlignmentX(1.0f);
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1
				.setHorizontalGroup(gl_panel_1
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_panel_1
										.createSequentialGroup()
										.addGap(4)
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																gl_panel_1
																		.createSequentialGroup()
																		.addComponent(
																				label,
																				GroupLayout.PREFERRED_SIZE,
																				55,
																				GroupLayout.PREFERRED_SIZE)
																		.addGap(47)
																		.addComponent(
																				comboBox,
																				GroupLayout.PREFERRED_SIZE,
																				70,
																				GroupLayout.PREFERRED_SIZE))
														.addGroup(
																gl_panel_1
																		.createSequentialGroup()
																		.addComponent(
																				label_1,
																				GroupLayout.PREFERRED_SIZE,
																				55,
																				GroupLayout.PREFERRED_SIZE)
																		.addGap(47)
																		.addComponent(
																				comboBox_1,
																				GroupLayout.PREFERRED_SIZE,
																				119,
																				GroupLayout.PREFERRED_SIZE))
														.addGroup(
																gl_panel_1
																		.createSequentialGroup()
																		.addComponent(
																				label_2,
																				GroupLayout.PREFERRED_SIZE,
																				90,
																				GroupLayout.PREFERRED_SIZE)
																		.addGap(12)
																		.addComponent(
																				comboBox_2,
																				GroupLayout.PREFERRED_SIZE,
																				119,
																				GroupLayout.PREFERRED_SIZE))
														.addGroup(
																gl_panel_1
																		.createSequentialGroup()
																		.addComponent(
																				label_3,
																				GroupLayout.PREFERRED_SIZE,
																				90,
																				GroupLayout.PREFERRED_SIZE)
																		.addGap(12)
																		.addComponent(
																				comboBox_3,
																				GroupLayout.PREFERRED_SIZE,
																				119,
																				GroupLayout.PREFERRED_SIZE))
														.addGroup(
																gl_panel_1
																		.createSequentialGroup()
																		.addComponent(
																				label_4,
																				GroupLayout.PREFERRED_SIZE,
																				90,
																				GroupLayout.PREFERRED_SIZE)
																		.addGap(12)
																		.addComponent(
																				comboBox_4,
																				GroupLayout.PREFERRED_SIZE,
																				119,
																				GroupLayout.PREFERRED_SIZE))
														.addComponent(
																checkBox,
																GroupLayout.PREFERRED_SIZE,
																199,
																GroupLayout.PREFERRED_SIZE))));
		gl_panel_1
				.setVerticalGroup(gl_panel_1
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_panel_1
										.createSequentialGroup()
										.addGap(4)
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																gl_panel_1
																		.createSequentialGroup()
																		.addGap(5)
																		.addComponent(
																				label))
														.addComponent(
																comboBox,
																GroupLayout.PREFERRED_SIZE,
																26,
																GroupLayout.PREFERRED_SIZE))
										.addGap(7)
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																gl_panel_1
																		.createSequentialGroup()
																		.addGap(5)
																		.addComponent(
																				label_1))
														.addComponent(
																comboBox_1,
																GroupLayout.PREFERRED_SIZE,
																26,
																GroupLayout.PREFERRED_SIZE))
										.addGap(2)
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																gl_panel_1
																		.createSequentialGroup()
																		.addGap(5)
																		.addComponent(
																				label_2))
														.addComponent(
																comboBox_2,
																GroupLayout.PREFERRED_SIZE,
																26,
																GroupLayout.PREFERRED_SIZE))
										.addGap(2)
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																gl_panel_1
																		.createSequentialGroup()
																		.addGap(5)
																		.addComponent(
																				label_3))
														.addComponent(
																comboBox_3,
																GroupLayout.PREFERRED_SIZE,
																26,
																GroupLayout.PREFERRED_SIZE))
										.addGap(1)
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																gl_panel_1
																		.createSequentialGroup()
																		.addGap(5)
																		.addComponent(
																				label_4))
														.addComponent(
																comboBox_4,
																GroupLayout.PREFERRED_SIZE,
																26,
																GroupLayout.PREFERRED_SIZE))
										.addGap(7)
										.addComponent(checkBox,
												GroupLayout.PREFERRED_SIZE, 18,
												GroupLayout.PREFERRED_SIZE)));
		panel_1.setLayout(gl_panel_1);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE)
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 192, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(441, Short.MAX_VALUE))
		);
		panel.setLayout(gl_panel);
		contentPane.setLayout(gl_contentPane);

	}

	private class SwingAction extends AbstractAction {
		public SwingAction() {
			putValue(NAME, "Open a media file");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}

		public void actionPerformed(ActionEvent e) {
			
			fileChooser.setApproveButtonText("Play");
			fileChooser.addChoosableFileFilter(SwingFileFilterFactory
					.newVideoFileFilter());
			fileChooser.addChoosableFileFilter(SwingFileFilterFactory
					.newAudioFileFilter());
			FileFilter defaultFilter = SwingFileFilterFactory.newMediaFileFilter();
			fileChooser.addChoosableFileFilter(defaultFilter);
			fileChooser.setFileFilter(defaultFilter);

			
			if (JFileChooser.APPROVE_OPTION == fileChooser
					.showOpenDialog(Transcriber5.this)) {
				playerTest.playMedia(fileChooser.getSelectedFile()
						.getAbsolutePath());
				//remove file filter
				fileChooser.removeChoosableFileFilter(defaultFilter);

			}
		}
	}

	class SharedListSelectionHandler implements ListSelectionListener {
		public void valueChanged(ListSelectionEvent e) {
			ListSelectionModel lsm = (ListSelectionModel) e.getSource();

			int firstIndex = e.getFirstIndex();
			int lastIndex = e.getLastIndex();
			boolean isAdjusting = e.getValueIsAdjusting();
			// System.out.println("Event for indexes " + firstIndex + " - " +
			// lastIndex
			// + "; isAdjusting is " + isAdjusting + "; selected indexes:");

			if (isAdjusting) {
				if (lsm.isSelectionEmpty()) {
					// output.append(" <none>");
				} else {
					// Find out which indexes are selected.
					int minIndex = lsm.getMinSelectionIndex();
					int maxIndex = lsm.getMaxSelectionIndex();

					DefaultTableModel ourModel = (DefaultTableModel) table
							.getModel();

					int noRows = ourModel.getRowCount();

					if (maxIndex < (noRows - 1)) {
						Float utteranceTime = (Float) ourModel.getValueAt(
								maxIndex, 0);
						// System.out.println("Skipping to" +
						// ourModel.getValueAt(maxIndex, 0).toString());
						playerTest.setCurrentTime(utteranceTime);
					}

				}
			}

		}
	}
}
